package cn.newtouch.system.controller;

import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.system.domain.SystemDictionaryType;
import cn.newtouch.system.query.SystemDictionaryTypeQuery;
import cn.newtouch.system.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
@Api(value = "数据字典类型的API",description="类型相关的CRUD功能")
public class SystemDictionaryTypeController {

    @Autowired
    private ISystemDictionaryTypeService systemdictionarytypeService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有类型" )
    public List<SystemDictionaryType> findAll(){
        return systemdictionarytypeService.loadAll();
    }
    /**
     * 查询一个
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SystemDictionaryType findOne(@PathVariable Long id){

        return systemdictionarytypeService.loadById(id);
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            systemdictionarytypeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 添加修改
     * @return
     */
    @PutMapping
    @ApiOperation(value = "没有参数ID是添加或者有ID是修改" )
    public JsonResult updateOrAdd(@RequestBody SystemDictionaryType systemdictionarytype){
        try {
            if(systemdictionarytype.getId()==null){
                //添加操作
                systemdictionarytypeService.save(systemdictionarytype);
            }else{
                //修改操作
                systemdictionarytypeService.update(systemdictionarytype);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery query){
        try {
            return systemdictionarytypeService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }

    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            systemdictionarytypeService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }


}
